//
//  CoolDevice.m
//  HX2Car
//
//  Created by Mudmen on 2020/5/15.
//  Copyright © 2020 Hx2car_mac. All rights reserved.
//

#import "CoolDevice.h"
#import <SAMKeychain/SAMKeychain.h>
#import <SAMKeychainQuery.h>

static  CGFloat             sSystemVersion = 0.0;

@implementation CoolDevice

+ (NSString *)userAgent
{
    NSString *userAgent = [NSString stringWithFormat:@"%@/%@ (%@; iOS %@; Scale/%0.2f)", [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleExecutableKey] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleIdentifierKey], [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleVersionKey], [[UIDevice currentDevice] model], [[UIDevice currentDevice] systemVersion], [[UIScreen mainScreen] scale]];
    return userAgent;
}

+ (NSString*)UUID {
    CFUUIDRef uuid = CFUUIDCreate(NULL);
    NSString *uuidStr = CFBridgingRelease(CFUUIDCreateString(NULL, uuid));
    CFRelease(uuid);
    NSString *bundleId = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIdentifier"];
    NSString *identifierNumber = [SAMKeychain passwordForService:bundleId account:@"hx2car"];
    if (!identifierNumber){
        [SAMKeychain setPassword: [NSString stringWithFormat:@"%@", uuidStr] forService:bundleId account:@"hx2car"];
        identifierNumber = [SAMKeychain passwordForService:bundleId account:@"hx2car"];
    }
    return identifierNumber;
}

+ (NSString*)deviceToken {
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"DeviceToken"];
}
    
+ (void)saveDeviceToken:(NSData*)deviceTokenData {
    if (@available(iOS 13.0,*)) {
        if ([deviceTokenData description].length > 0) {
            const unsigned *tokenBytes = [deviceTokenData bytes];
            NSString *token = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
            ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
            ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
            ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
            [[NSUserDefaults standardUserDefaults] setObject:token forKey:@"DeviceToken"];
        }
    } else {
        NSMutableString *mutableDeviceToken = [NSMutableString stringWithString:[deviceTokenData description]];
        LogInfo(@"Raw DeviceToken:%@", mutableDeviceToken);
        if ([mutableDeviceToken length] > 0) {
            if ([mutableDeviceToken characterAtIndex:0] == '<') {
                [mutableDeviceToken deleteCharactersInRange:NSMakeRange(0, 1)];
            }
            if ([mutableDeviceToken length] > 0) {
                if ([mutableDeviceToken characterAtIndex:[mutableDeviceToken length]-1] == '>') {
                    [mutableDeviceToken deleteCharactersInRange:NSMakeRange([mutableDeviceToken length]-1, 1)];
                }
            }
            if ([mutableDeviceToken length] > 0) {
                for (NSUInteger index = 0; index < [mutableDeviceToken length]; index++) {
                    if ([mutableDeviceToken characterAtIndex:index] == ' ') {
                        [mutableDeviceToken deleteCharactersInRange:NSMakeRange(index, 1)];
                        index = 0;
                    }
                }
            }
        }
        LogInfo(@"Avalaible DeviceToken:%@", mutableDeviceToken);
        [[NSUserDefaults standardUserDefaults] setObject:mutableDeviceToken forKey:@"DeviceToken"];
    }
}

+ (CGFloat)systemVersion {
    if (sSystemVersion == 0.0f) {
        sSystemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
    }
    return sSystemVersion;
}


@end
